package cn.syx.comtroller;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.image.ImageClient;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiAudioTranscriptionClient;
import org.springframework.ai.openai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.openai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    @Autowired
    private ChatClient chatClient;

    @GetMapping("/chat")
    public String generate(@RequestParam String message) {

        return chatClient.call(message);
    }

    @Autowired
    private ImageClient imageClient;

    @GetMapping("/image")
    public String image(@RequestParam String message) {
        ImagePrompt imagePrompt = new ImagePrompt(message);
        ImageResponse imageResponse = imageClient.call(imagePrompt);
        return imageResponse.getResult().getOutput().getUrl();
    }


    @Value("classpath:/jfk.flac")
    private Resource audioFile;

    @Autowired
    private OpenAiAudioTranscriptionClient audioTranscriptionClient;


    @GetMapping("/audio")
    public String audio() {
        AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFile);
        AudioTranscriptionResponse response = audioTranscriptionClient.call(transcriptionRequest);
        return response.getResult().getOutput();
    }

}
